﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Projectmanagement.Entity;
using ProjectManagement.Models;
using Projectmanagement.Utl;

namespace Projectmanagement.Controllers
{
    [RoleRequired]
    public class FullCalendarController : Controller
    {
        public ActionResult Index()
        {
            return View(DateTime.Now);
        }

        public ActionResult freshFullCalendar(string searchdt, string searchValue, string searchProject)
        {
            DateTime dt = string.IsNullOrWhiteSpace(searchdt) ? DateTime.Now : DateTime.ParseExact(searchdt, "ddMMyyyy", System.Globalization.CultureInfo.CurrentCulture);

            if (searchValue == "-请选择-")
            {
                searchValue = string.Empty;
            }
            if (searchProject == "-请选择-")
            {
                searchProject = string.Empty;
            }
            if (string.IsNullOrEmpty(searchValue) && string.IsNullOrEmpty(searchProject))
            {
                return PartialView("_PartialCalendar", dt);
            }
            else
            {
                var pro = Helpers.GetUserProjects();
                if (!string.IsNullOrWhiteSpace(searchProject))
                {
                    int projectID = Convert.ToInt32(searchProject);
                    pro = pro.Where(s => s.ID == projectID);
                }

                if (Employee.CurrentRoleLVL >= RoleLevel.Manager)
                {
                    var ts = CH.DB.Tasks.Where(t => pro.Any(p => p.ID == t.ProjectID));
                    if (!string.IsNullOrWhiteSpace(searchValue))
                    {
                        ts = ts.Where(s => s.Owners.Contains(searchValue));
                    }
                    ViewBag.tasks = ts;
                }
                else
                {
                    var ts = CH.DB.Tasks.Where(t => pro.Any(p => p.ID == t.ProjectID) && t.Owners.Contains(Employee.CurrentUserName));
                    ViewBag.tasks = ts;
                }
                return PartialView("_PartialCalendar", dt);
            }
        }


        [HttpPost]
        public ActionResult SaveTask(int id)
        {
            var item = CH.GetDataByID<Task>(id);
            if (TryUpdateModel(item))
            {
                if (item.ProjectID != null)
                {
                    var project = CH.DB.Projects.Find(item.ProjectID);
                    if (project != null)
                    {
                        item.Assinger = project.Manager;
                    }
                }
                CH.Edit<Task>(item);
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult SaveState(int id)
        {
            var item = CH.GetDataByID<Task>(id);
            if (TryUpdateModel(item))
            {
                if (item.ProjectID != null)
                {
                    var project = CH.DB.Projects.Find(item.ProjectID);
                    if (project != null)
                    {
                        item.Assinger = project.Manager;
                    }
                }
                CH.Edit<Task>(item);
            }
            return Json("ok");
        }

        [HttpPost]
        public ActionResult GetRole()
        {
            var rolename = Employee.GetRoleName(Employee.CurrentUserName);
            return Json(rolename, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetMember(int name)
        {
            var member = GetMemberSelectListItems(name);
            return Json(member, JsonRequestBehavior.AllowGet);
        }

        public IEnumerable<SelectListItem> GetMemberSelectListItems(int name)
        {
            var ss = from m in CH.DB.Members
                     join p in CH.DB.Projects on m.ProjectID equals p.ID
                     where p.ID == name
                     select m;
            var selectList = ss.ToList().Select(a => new SelectListItem
            {
                Text = a.Name,
                Value = a.ID.ToString()
            });
            return selectList;
        }

        public JsonResult MemberFilter(int? projectid)
        {
            if (Employee.CurrentRoleLVL >= 100)
            {
                var ms = from c in CH.DB.Members
                         where c.ProjectID == projectid && c.IsActivated
                         select new { text = c.Name, value = c.Name };
                return Json(ms, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var ms = from c in CH.DB.Members
                         where c.ProjectID == projectid && c.Name == Employee.CurrentUserName
                         select new { text = c.Name, value = c.Name };
                return Json(ms, JsonRequestBehavior.AllowGet);
            }

        }

        public JsonResult ProjectFilter()
        {
            var ps = Helpers.GetUserProjects().Select(s =>
                new
                {
                    text = s.ProjectCode,
                    value = s.ID
                });
            return Json(ps, JsonRequestBehavior.AllowGet);
        }
    }
}
